Imports Cdm.DataCore.UserSecurity
Imports System.Windows.Forms

Partial Class MasterPageExpandable
    Inherits System.Web.UI.MasterPage

    Private hideHeadersActive As Boolean = False
    Private resizeDivs As Boolean = True

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        If UserAuthorization.CurrentUser Is Nothing Then
            'test wfy
            Response.Redirect("~/login.aspx")
        ElseIf UserAuthorization.CurrentUser.PasswordExpires < System.DateTime.Now Then
            Response.Redirect("~/changepassword.aspx")
        End If

        CheckExpire()

        Session("ChildDivs") = ""
        Session("ChildDivSpacing") = ""
    End Sub
    ''' <summary>
    ''' Check if either the session expires, or the last page load was more than the inactivity period.
    ''' If either is the case, redirect the user to the login page.
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub CheckExpire()
        If Session("lastpageload") IsNot Nothing Then
            Dim lastPageLoadDate As DateTime = Session("lastpageload")
            If DateDiff(DateInterval.Minute, lastPageLoadDate, System.DateTime.Now) > EntityController.GetInactivePeriod Then
                Session("lastpageload") = Nothing
                Session("CurrentUser") = Nothing
                Response.Redirect("~/login.aspx")
            End If
        End If

        Session("lastpageload") = System.DateTime.Now()
    End Sub


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        'MessageBox.Show("isPostBack=" + Me.IsPostBack.ToString())

        If Not Me.IsPostBack Then

            Me.Page.ClientScript.RegisterClientScriptInclude("AssessmentScripts", "AssessmentScripts.js")

            PrintLink.NavigateUrl = Request.Url.PathAndQuery
            If EntityController.IsSingleParticipantUser() Then
                Me.toolbarTable.Style("BACKGROUND-IMAGE") = "url(/CDMWeb/images/navbarblue.png)"
            Else
                Me.toolbarTable.Style("BACKGROUND-IMAGE") = "url(/CDMWeb/images/navbarblue2.png)"
            End If
            'PrintLinkButton.PostBackUrl = Me.CurrentPageURL

            If Request.QueryString().ToString() = "" Then
                PrintLink.NavigateUrl = PrintLink.NavigateUrl + "?isprintpage=true"
            Else
                If Request.QueryString("isprintpage") = "true" Then
                    PrintLink.Visible = False
                    CloseWindowLink.Visible = True
                Else
                    PrintLink.NavigateUrl = PrintLink.NavigateUrl + "&isprintpage=true"
                End If

            End If


            Dim roleId As Integer = UserAuthorization.CurrentUser.RoleId
            Dim itemList As System.Collections.Generic.List(Of Cdm.DataCore.Navigation.ToolbarItem) = Cdm.DataCore.Navigation.ToolbarItem.ListByRole(roleId)

            'Sys Admin user account
            If Not IsParticipantUser() Then
                ' Me.PreferencesHyperLink.Visible = False
                Me.NavPref.Visible = False

                'Me.lnkHelp.Visible = False
                Me.NavHelp.Visible = False
                Me.ChangePasswordLink.Visible = False
                Me.LinkHelp.Visible = False
                ' Me.lnkLogout.Visible = False
                Me.LogoutSpan.Visible = False
                Me.LinkLogout.Visible = False
                Me.NavLogOut.Visible = False
            End If

            If IsParticipantUser() Then
                Me.ParticipantToolbar1.IsSmallToolbar = False
                Me.NavigationImage.ImageUrl = "~/images/navbarblue.png"
                'Me.pnlPage.Height = 98
                Me.pnlPage.Style("top") = "76px"
            Else
                Me.ParticipantToolbar1.IsSmallToolbar = True
                Me.pnlPage.Style("top") = "63px"
            End If
            Me.ParticipantToolbar1.Itemlist = itemList

            'Footer information
            If Application("CCLink") = "true" Then
                lnkCC.Visible = True

                Dim urlObject As Cdm.DataCore.LookupTables.LuApplicationSettings
                urlObject = Cdm.DataCore.LookupTables.LuApplicationSettings.Retrieve("CashAndCounselingLinkText")

                lnkCC.Text = urlObject.SettingValue

                urlObject = Cdm.DataCore.LookupTables.LuApplicationSettings.Retrieve("CashAndCounselingLink")

                lnkCC.NavigateUrl = urlObject.SettingValue
            End If

            lblFooter.Text = Application("Footer")
            'Me.ParticipantToolbar1.Itemlist = itemList
            'Me.ParticipantToolbar1.SelectedItem = "Edit"

            If Request("isprintpage") = "true" Then
                HideHeader(True)
            ElseIf (Not hideHeadersActive) And resizeDivs Then
                Me.MasterPageBody.Attributes.Add("onload", "SetPageDivs();")
                Me.MasterPageBody.Attributes.Add("onresize", "SetPageDivs();")
            Else
                'Do nothing - this is for pages that hide the header
                ' but are not a print page.  Keeps the links on the page enabled
                ' and do not resize any parts.
            End If

        End If

            SetHiddenFields()
    End Sub

    Public Sub HideHeader(ByVal disableLinks As Boolean)
        NavigationImage.Visible = False
        ParticipantToolbar1.Visible = False
        hideHeadersActive = True
        If Not Me.MasterPageBody.Attributes("onload") Is Nothing Then
            Me.MasterPageBody.Attributes.Remove("onload")
            Me.MasterPageBody.Attributes.Remove("onresize")
        End If
        pnlPage.Visible = False
        If (disableLinks) Then
            lnkCC.Enabled = False
            lnkVersion.Enabled = False
            AppHeaderWithTables1.SetPagePrintable()
            Me.PrintLink.Enabled = True
            Me.CloseWindowLink.Enabled = True

            Me.MasterPageBody.Attributes.Add("onload", "SetPrintablePage();")
        End If
        
    End Sub

    Public Sub SetResizeDivs(ByVal isResize As Boolean)
        If isResize = False Then
            If Not Me.MasterPageBody.Attributes("onload") Is Nothing Then
                Me.MasterPageBody.Attributes.Remove("onload")
                Me.MasterPageBody.Attributes.Remove("onresize")
            End If
        End If

        Me.resizeDivs = isResize

    End Sub

    Private Sub SetHiddenFields()
        If Not Session("FixedDivHeight") Is Nothing Then
            Me.FixedDivHeightHidden.Value = Session("FixedDivHeight").ToString()
        End If
        If Not Session("ChildDivs") Is Nothing Then
            Me.ChildDivListHidden.Value = Session("ChildDivs").ToString()
        End If

        If Not Session("ChildDivSpacing") Is Nothing Then
            Me.ChildDivSpacingHidden.Value = Session("ChildDivSpacing").ToString()
        End If

    End Sub

    Private Function IsParticipantUser() As Boolean
        Select Case (UserAuthorization.CurrentUser.RoleId)
            Case CInt(Role.RoleType.Participant), CInt(Role.RoleType.Representative), CInt(Role.RoleType.LegalGuardian)
                Return True
            Case Else
                Return False
        End Select
    End Function
End Class

